package ru.spb.osll.myfit.server.execute;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.File;

public class PageWriter {
  private final String head = new String( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n"
					+ "   \"http://www.w3.org/TR/html4/strict.dtd\">\n"
					+ "<HTML>\n"
					+ "  <HEAD>\n"
					+ "    <LINK href=\"files" + File.separator + "css" + File.separator +
									"fitnesse_base.css\" rel=\"stylesheet\" type=\"text/css\">\n"
					+ "    <SCRIPT src=\"files" + File.separator + "javascript" + File.separator +
									"fitnesse.js\"></SCRIPT>\n"
					+ "  </HEAD>\n"
					+ "  <BODY>\n");
  private final String tail = new String( "  </BODY>\n"
					+ "</HTML>\n");

	private String m_pageName;

	public PageWriter(String pageName) {
		m_pageName = pageName;
	}

	public void writeStream(ByteArrayOutputStream outputStream) {
		String strPage = outputStream.toString();
    try {
      outputStream.close();

      try {

        FileOutputStream out = new FileOutputStream(m_pageName);
        try {
          OutputStreamWriter outwriter = new OutputStreamWriter(out, "windows-1251");
          PrintWriter writer = new PrintWriter(outwriter);
	  writer.write(head);
          writer.write(strPage);
	  writer.write(tail);
          writer.close();
          outwriter.close();
          out.close();

        } catch (UnsupportedEncodingException e) {
          out.close();
        }
      } catch (FileNotFoundException e) {

      }

    } catch (IOException e) {
    }

	}
}
